
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.junit.Before;
import org.junit.Test;

import java.io.*;
import java.net.URI;

public class HdfsAPI {
    FileSystem fs;

    @Before
    //1、获取hdfs文件管理系统 file system
    public  void main()throws Exception {
        //获取配置文件
        Configuration configuration = new Configuration();
        configuration.set("dfs.replication","1");

        //获取（指定）连接地址 hdfs的地址
        URI uri = new URI("hdfs://master:9000");

        //获取（创建）hdfs文件管理系统的对象，通过这个对象操作hdfs
        fs = FileSystem.get(uri, configuration);

    }

    @Test
    public void mk()throws Exception{
        //创建一个目录
        boolean mkdirs = fs.mkdirs(new Path("/test/test/test"));
        System.out.println(mkdirs);
    }

    @Test
    public void del() throws Exception{
        //boolean表示是否可以迭代删除；true可以 false不行
        boolean delete = fs.delete(new Path("/test"), true);

        System.out.println(delete);
    }

    @Test
    public void listStatus() throws IOException {

        //查看目录下文件列表
        FileStatus[] fileStatuses = fs.listStatus(new Path("/data"));

        //获取一个一个文件
        for (FileStatus fs :fileStatuses){

            System.out.println(fs.getBlockSize());
            System.out.println(fs.getLen());
            System.out.println(fs.getReplication());
            System.out.println(fs.getPermission());
            System.out.println(fs.getPath());
            System.out.println("----------------");


        }


    }

    @Test
    public void listBlockLocation()throws Exception{

        BlockLocation[] fb1 = fs.getFileBlockLocations(new Path("/data/student.txt"),0,10);

        for (BlockLocation blockLocation : fb1) {
            String[] hosts = blockLocation.getHosts();
            for (String host : hosts) {
                System.out.println(host);
            }
            System.out.println();
            System.out.println(blockLocation.getLength());
            System.out.println();
            String[] names = blockLocation.getNames();
            for (String name : names) {
                System.out.println(name);
            }
            System.out.println(blockLocation.getOffset());
            System.out.println();
            String[] topologyPaths = blockLocation.getTopologyPaths();
            for (String topologyPath : topologyPaths) {
                System.out.println(topologyPath);

            }
        }

    }

    @Test
    public void open()throws Exception{
        FSDataInputStream open = fs.open(new Path("/data/student.txt"));

        //缓冲流
        BufferedReader br = new BufferedReader(new InputStreamReader(open));
        String line;
        while ((line=br.readLine())!=null){
            System.out.println(line);
        }
        br.close();
        open.close();

    }

    //hdfs上的文件  默认不能被修改
    //想实现修改  只能删除  重新上传
    @Test
    public void create()throws Exception{
        FSDataOutputStream fsDataOutputStream = fs.create(new Path("/data/students1.txt"));

        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fsDataOutputStream));
        bw.write("你好");
        bw.write("世界");
        bw.close();
        fsDataOutputStream.close();

    }

}
